<template>
  <div>
      <el-dialog 
      width="50%"
      :title="textMap[dialogStatus]" 
      :visible.sync="DialogFormVisibleCom"
      append-to-body
      >
      <el-form ref="dataForm" :rules="rules" :model="formSubmitDataCom" label-position="right" label-width="100px" style="width: 90%; margin:10px auto;">
      
        <el-row :gutter="20">

     


          <el-col :span="12">
            <!-- 生效日期 -->
            <el-form-item label="生效日期：" prop="s_date">
              <el-date-picker 
              v-model="formSubmitDataCom.s_date" 
              type="date" 
              placeholder="Please pick a date"
              value-format="yyyy-MM-dd"
              />
            </el-form-item>
          </el-col>
        
          <el-col :span="12">
            <!-- 失效日期 -->
            <el-form-item label="失效日期" prop="e_date">
                <el-date-picker 
                  v-model="formSubmitDataCom.e_date" 
                  :disabled="dialogStatus==='create'"
                  type="date" 
                  placeholder="Please pick a date"
                  value-format="yyyy-MM-dd"
                  />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <!-- 失效日期 -->
            <el-form-item label="期限编号" prop="term_code">
                <el-select v-model="formSubmitDataCom.term_code" multiple placeholder="请选择">
                  <el-option
                    v-for="item in termCodeOptions"
                    :key="item.term_code"
                    :label="item.term_name"
                    :value="item.term_code">
                  </el-option>
                </el-select>
            </el-form-item>
          </el-col>
         
        </el-row>


      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button @click="resetFormData">
          重置
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          提交
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// import PageHeaderLayout from '@/layouts/PageHeaderLayout'
// import ApeDrawer from '@/components/ApeDrawer'
// import { mapGetters } from 'vuex'
// const calendarTypeOptions = [
//   { key: 'lp_code', display_name: 'lp_name' },
// ]
 
// // arr to obj, such as { CN : "China", US : "USA" }
// const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
//   acc[cur.key] = cur.display_name
//   return acc
// }, {})

import XEUtils from 'xe-utils'


export default {
  name: 'TimeLimitSubmitForm',
  components: {
    //PageHeaderLayout,
  },
  props:{
    dialogFormVisible:{
        type: Boolean,
        default: false
    },
    dialogStatus:{
        type: String,
    },
    termCodeOptions: {
        type: Array,
        default() {
          return []
        }
    },
    formSubmitData:{
      type: Object,
      default: function() {
        return {
          s_date:'',
          e_date:'',
          term_code:[]
        }
      }
    },
  },
  // 定义属性
  data() {
    /* 生效日期验证 */
    var validateDateRangeSdate = (rule, value, callback) => {
      if (XEUtils.isEmpty(value)) {
        callback(new Error('生效日期必填!'));
       } else if ((!XEUtils.isEmpty(this.formSubmitDataCom.e_date)) && (value > this.formSubmitDataCom.e_date)) {
        callback(new Error('生效日期必须小于失效日期!'));
      }else{
          callback();
      }
    };
    /* 失效日期验证 */
    var validateDateRangeEdate = (rule, value, callback) => {
      if (XEUtils.isEmpty(value)) {
        callback(new Error('失效日期必填!'));
      } else if ((!XEUtils.isEmpty(this.formSubmitDataCom.s_date)) && (value < this.formSubmitDataCom.s_date)) {
        callback(new Error('失效日期必须大于生效日期!'));
      }else{
        callback();
      }
    };

    return {
      textMap: {
          update: '修改期限',
          create: '新增期限'
      },
      rules: {
        s_date: [{ validator: validateDateRangeSdate, trigger: 'change'}],
        e_date: [{ validator: validateDateRangeEdate, trigger: 'change'}],
        // title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      // 表单用到的值
      // calendarTypeOptions,
      // statusOptions: ['published', 'draft', 'deleted'],
    }
  },
  // 计算属性，会监听依赖属性值随之变化
  computed: {
    DialogFormVisibleCom:{
      get() {
        return this.dialogFormVisible
      },
      set(val) {
        this.$emit('update:dialogFormVisible', val)
      }
    },
    formSubmitDataCom:{
      get() {
        return this.formSubmitData
      },
      set(val) {
        this.$emit('update:formSubmitData', val)
      }
    }
  },
  // 监控data中的数据变化
  watch: {},
  // 方法集合
  methods: {
     resetFormData(){
       if (this.dialogStatus == 'create') {
          this.$emit('resetFormDataEmit',{
          replaceObj:{
            s_date: '',
            e_date: '9999-12-31'
          }
        })
       }else{
         this.$emit('resetFormDataEmit')
       }
      
    },


    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$emit('handleFromCreateDataEmit')
        }
      })
    },

    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$emit('handleFromUpdateDataEmit')
        }
      })
    },
    
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    
  },
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {}, // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>

<style lang='scss' scoped>
  
</style>